package com.web.hotel.action;

import com.opensymphony.xwork2.ModelDriven;
import com.util.BaseActionUtil;
import com.util.JsonUtil;
import com.web.hotel.po.City;
import com.web.hotel.po.Region;
import com.web.hotel.service.CityService;
import com.web.system.po.UserPo;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.*;

@Controller
@Scope("prototype")
@ParentPackage(value = "struts-default")
@Namespace(value = "/")
@Action(value = "cityAction", results = {
        @Result(name = "index", location = "/jsp/hotel/city_list.jsp"),
        @Result(name = "addPage", location = "/jsp/hotel/city_add.jsp")
})
public class CityAction extends BaseActionUtil implements ModelDriven<City> {

    private City city;
    @Resource
    private CityService cityService;

    public String execute() throws Exception {
        return null;
    }


    public City getModel() {
        city = new City();
        return city;
    }

    /**
     * 获得城市名称
     *
     * @return
     */
    public String getCityName() {
        String cityName = request.getParameter("q");
        List<City> cityList = cityService.getCityByName(cityName);
        String jsonStr = JsonUtil.objectToStr(cityList);
        System.out.println(jsonStr);
        out.print(jsonStr);
        out.flush();
        out.close();
        return NONE;
    }
    public String getAreaName()throws Exception{
        String cityName = request.getParameter("cityName");
        String landmarks = request.getParameter("landmarks");
        List<City> cities = cityService.getCityByLandMarks(cityName,landmarks);
        JsonUtil.jsonOut(cities);
        return NONE;
    }
    /**
     * 获得所有城市名称
     *
     * @return
     */
    public String getAllCityName() {
        String cityName = request.getParameter("q");
        List<Region> cityList = cityService.getAllCityByName(cityName);
        String jsonStr = JsonUtil.objectToStr(cityList);
        System.out.println(jsonStr);
        out.print(jsonStr);
        out.flush();
        out.close();
        return NONE;
    }

    public String index() {
        return "index";
    }

    public String addPage() {
        return "addPage";
    }

    public String listGrid() throws Exception {
        Map<String, Object> dataMap = cityService.listGrid(request);
        String jsonStr = JsonUtil.objectToStr(dataMap);
        out.print(jsonStr);
        out.flush();
        out.close();
        return NONE;
    }

    public String deleteCityById() throws Exception {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        String cityIds = request.getParameter("ids");
        if (cityIds == null || "".equals(cityIds)) {
            dataMap.put("flag", false);
            dataMap.put("msg", "服务端未接收到客户端传递的数据");
        } else {
            dataMap = cityService.deleteCityById(cityIds);
        }
        String jsonStr = JsonUtil.objectToStr(dataMap);
        out.print(jsonStr);
        out.flush();
        out.close();
        return NONE;
    }

    public String addCity() throws Exception {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        if (city == null || "".equals(city)) {
            dataMap.put("flag", false);
            dataMap.put("msg", "服务端未接收到客户端传递的数据");
        } else {
            UserPo user = (UserPo) session.getAttribute("user");
            String createUser;
            if (user != null && !"".equals(user)) {
                createUser = user.getUserName();
                Date createTime = new Date();
                city.setCreateTime(createTime);
                city.setCreateUser(createUser);
                dataMap = cityService.addCity(city);
            } else {
                dataMap.put("flag", false);
                dataMap.put("msg", "服务端未接收到客户端传递的数据");
            }


        }
        String jsonStr = JsonUtil.objectToStr(dataMap);
        out.print(jsonStr);
        out.flush();
        out.close();
        return NONE;
    }

    public String getCityById() throws Exception {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        String cityId = request.getParameter("cityId");
        if (cityId == null || "".equals(cityId)) {
            dataMap.put("flag", false);
            dataMap.put("msg", "服务端未接收到客户端传递的数据 ");
        } else {
            Integer int_cityId = Integer.parseInt(cityId);
            City cityI = cityService.getCityById(int_cityId);
            dataMap.put("flag", true);
            dataMap.put("msg", cityI);
        }
        String jsonStr = JsonUtil.objectToStr(dataMap);
        System.out.println(jsonStr);
        out.print(jsonStr);
        out.flush();
        out.close();
        return NONE;
    }

    public String editCity() throws Exception {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        if (city == null || "".equals(city)) {
            dataMap.put("flag", false);
            dataMap.put("msg", "服务端未接收到客户端传递的数据");
        } else {
            dataMap = cityService.editCity(city);
        }
        String jsonStr = JsonUtil.objectToStr(dataMap);
        System.out.println(jsonStr);
        out.print(jsonStr);
        out.flush();
        out.close();
        return NONE;

    }

    public String getLandmarks() {
        String cityName = request.getParameter("cityName");
        List<String> landmarks = cityService.getLandmarks(cityName);
        List<Map<String,Object>> landmarksMap = new ArrayList<Map<String, Object>>();
        for (String landmark : landmarks) {
            Map<String,Object> objectMap = new HashMap<String, Object>();
            objectMap.put("landmarks",landmark);
            landmarksMap.add(objectMap);
        }
        JsonUtil.jsonOut(landmarksMap);
        return NONE;
    }

    public String isExistArea() throws Exception {
        String areaName = request.getParameter("areaName");
        City city = cityService.getCityByAreaName(areaName);
        Boolean flag = true;
        if (city != null && !"".equals(city)) {
            flag = false;
        } else {
            flag = true;
        }
        String jsonStr = JsonUtil.objectToStr(flag);
        System.out.println(jsonStr);
        out.print(jsonStr);
        out.flush();
        out.close();
        return NONE;
    }

}
